<script lang="ts" setup>
import type { VbenFormSchema } from '@vben/common-ui';

import { computed, markRaw } from 'vue';

import { AuthenticationLogin, SliderCaptcha, z } from '@vben/common-ui';
import { $t } from '@vben/locales';

import { getHasOauthLogin } from '#/api/plugin/wechat';
import { useAuthStore } from '#/store';

import ThirdPartyLogin from './third-party-login.vue';

defineOptions({ name: 'Login' });
const isThirdPartyLogin = ref(false);
const authStore = useAuthStore();

const formSchema = computed((): VbenFormSchema[] => {
  return [
    {
      component: 'VbenInput',
      componentProps: {
        placeholder: $t('authentication.usernameTip'),
      },
      fieldName: 'username',
      label: $t('authentication.username'),
      rules: z.string().min(1, { message: $t('authentication.usernameTip') }),
    },
    {
      component: 'VbenInputPassword',
      componentProps: {
        placeholder: $t('authentication.password'),
      },
      fieldName: 'password',
      label: $t('authentication.password'),
      rules: z.string().min(1, { message: $t('authentication.passwordTip') }),
    },
    {
      component: markRaw(SliderCaptcha),
      fieldName: 'captcha',
      rules: z.boolean().refine((value) => value, {
        message: $t('authentication.verifyRequiredTip'),
      }),
    },
  ];
});
onMounted(async () => {
  try {
    await getHasOauthLogin()
      .then((res) => {
        isThirdPartyLogin.value = res.data;
      })
      .catch(() => {
        // 不做任何操作
      });
  } catch {
    console.warn('没有开启第三方登录');
  }
});
</script>

<template>
  <AuthenticationLogin
    :form-schema="formSchema"
    :loading="authStore.loginLoading"
    :show-code-login="false"
    :show-forget-password="false"
    :show-qrcode-login="false"
    :show-register="false"
    :show-third-party-login="isThirdPartyLogin"
    @submit="authStore.authLogin"
  >
    <template #third-party-login>
      <ThirdPartyLogin v-if="isThirdPartyLogin" />
    </template>
  </AuthenticationLogin>
</template>
